package trading.ctm.yunjing.util

import android.content.Context
import android.graphics.Color
import android.os.CountDownTimer
import android.support.design.widget.TextInputEditText
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.ImageView
import android.widget.TextView

/**
 * dp2px
 */
fun dp2px(context: Context, dpValue: Float): Int {
    val scale = context.resources.displayMetrics.density
    return (scale * dpValue + 0.5f).toInt()
}

/**
 * px2dp
 */
fun px2dp(context: Context, pxValue: Float): Int {
    val scale = context.resources.displayMetrics.density
    return (pxValue / scale + 0.5f).toInt()
}

/**
 * 手机号校验
 */
fun isPhoneNumber(phoneNumber: String): Boolean {
    if (TextUtils.isEmpty(phoneNumber)) {
        return false
    } else {
        val str = phoneNumber.replace(" ", "")
        return str.matches(Regex("[1][3456789]\\d{9}"))
    }

}

/**
 * 账号校验
 */
fun isAccount(account: String): Boolean {
    return !TextUtils.isEmpty(account)
}

/**
 * 密码校验
 */
fun isPassword(password: String): Boolean {
    return !TextUtils.isEmpty(password) && password.matches(Regex("^[A-Za-z0-9'\"{}\\(\\)\\[\\]\\*&.@#$%^*()_+-=><?!,…:;]{4,12}$"))
}

/**
 * 验证码校验
 */
fun isCode(code: String): Boolean {
    return code.length == 6
}

/**
 * 倒计时
 */
fun countDownTime(tv: TextView, time: Long, str: String) {
    object : CountDownTimer(time, 1000L) {
        override fun onFinish() {
            tv.isClickable = true
            tv.text = "重新发送"
            this.cancel()
        }

        override fun onTick(millisUntilFinished: Long) {
            tv.isClickable = false
            tv.text = "" + millisUntilFinished / 1000 + str
        }

    }.start()
}

/**
 * 账号监听
 */
fun acountChange(eta: TextInputEditText, etp: TextInputEditText, iv: ImageView, tv: TextView) {
    eta.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            if (isAccount(s.toString())) {
                iv.visibility = View.VISIBLE
                if (isPassword(etp.text.toString())) {
                    tv.setBackgroundColor(Color.parseColor("#FF693C26"))
                } else {
                    tv.setBackgroundColor(Color.parseColor("#40693C26"))
                }
            } else {
                iv.visibility = View.GONE
                tv.setBackgroundColor(Color.parseColor("#40693C26"))
            }
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }

    })
}

/**
 * 密码监听
 */
fun passwordChange(etp: TextInputEditText, eta: TextInputEditText, iv: ImageView, tv: TextView) {
    etp.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            if (isPassword(s.toString()) && isAccount(etp.text.toString())) {
                tv.setBackgroundColor(Color.parseColor("#FF693C26"))
            } else {
                tv.setBackgroundColor(Color.parseColor("#40693C26"))
            }
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }

    })
}

/**
 * 手机号码监听
 */
fun phoneChange(etp: TextInputEditText, etc: TextInputEditText, ets: TextInputEditText, iv: ImageView, tv: TextView) {

    etp.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            if (isPhoneNumber(s.toString())) {
                iv.visibility = View.VISIBLE
                if (isCode(etc.text.toString()) && isPassword(ets.text.toString())) {
                    tv.setBackgroundColor(Color.parseColor("#FF693C26"))
                } else {
                    tv.setBackgroundColor(Color.parseColor("#40693C26"))
                }
            } else {
                iv.visibility = View.GONE
                tv.setBackgroundColor(Color.parseColor("#40693C26"))
            }
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }

    })
}

/**
 * 验证码监听
 */
fun codeChange(etc: TextInputEditText, etp: TextInputEditText, ets: TextInputEditText, tv: TextView) {

    etc.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {

            if (isCode(s.toString()) && isPhoneNumber(etp.text.toString()) && isPassword(ets.text.toString())) {
                tv.setBackgroundColor(Color.parseColor("#FF693C26"))
            } else {
                tv.setBackgroundColor(Color.parseColor("#40693C26"))
            }

        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }

    })
}

/**
 * 新密码监听
 */
fun passwordChange(ets: TextInputEditText, etp: TextInputEditText, etc: TextInputEditText, tv: TextView) {

    ets.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {

            if (isPassword(s.toString()) && isPhoneNumber(etp.text.toString()) && isCode(etc.text.toString())) {
                tv.setBackgroundColor(Color.parseColor("#FF693C26"))
//                tv.setEnable(true)
            } else {
                tv.setBackgroundColor(Color.parseColor("#40693C26"))
            }

        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }

    })
}


